package ru.spb.osll.myfit.client.widget;

import com.google.gwt.user.client.ui.Grid;
import ru.spb.osll.myfit.common.PageExecutingResult;
import ru.spb.osll.myfit.common.ProgressCounts;

/*
.progressbarFull {
 background: #33ff33;
}

.progressbarBlank {
 background: #d3d3d3;
}

.progressbarText {
 font-weight: bold;
 color: black;
 font-size: 12px;
}

.progressbarBorder {
 border-style: solid;
 border-color: black;
 border-width: 1;
}
 */

public class ProgressBar extends Grid implements PageExecutingListener {
	private int m_elements;

	public ProgressBar() {
		this(25);
	}

	public ProgressBar(int elements) {
        super(1, elements);
		setVisible(false);

		m_elements = elements;
		setCellPadding(0);
		setCellSpacing(0);

		for(int i = 0; i < m_elements; i++) {
			Grid e = new Grid(1, 1);
			e.setStyleName("progressbarBlank");
			setWidget(0, i, e);
		}

		setWidth("100%");
		setStyleName("progressbarBorder");

	}

	public void updateView(ProgressCounts pCount) {
		int total = pCount.getTotalSegments();
		int progress = pCount.getProgressSegments();

		double persentage = (double) progress / total;
		int progressCount = (int) (m_elements * persentage);

		for(int i = 0; i < m_elements; i++) {
			Grid e = (Grid) getWidget(0, i);
			if(i < progressCount)
				e.setStyleName("progressbarFull");
			else
				e.setStyleName("progressbarBlank");
		}

	}

  @Override
  public void processPageExecutingResult(PageExecutingResult result) {
    updateView(result.getCounts());
  }

  @Override
  public void finish() {
    
  }
}
